package com.cntaiping.ttnews.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cntaiping.ttnews.R;
import com.cntaiping.ttnews.adapter.MyMoreTypeAdapter;
import com.cntaiping.ttnews.bean.NewsBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyNewsFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private NewsBean mdatas;
    private RecyclerView rec;
    private MyMoreTypeAdapter adapter;


    public static MyNewsFragment newInstance(String param1) {
        MyNewsFragment fragment = new MyNewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            initData();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_mynews, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rec = (RecyclerView) view.findViewById(R.id.rec_news);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (mdatas != null) {
            adapter = new MyMoreTypeAdapter(getContext(), mdatas);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
            rec.setLayoutManager(layoutManager);
            rec.setAdapter(adapter);
            adapter.notifyDataSetChanged();


        }
    }


    //解析数据
    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(mParam1).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String string = response.body().string();
                final NewsBean bean = new Gson().fromJson(string, NewsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mdatas = bean;


                    }
                });

            }
        });
    }

    Handler handler = new Handler();
}
